# coding:utf-8
from models import meizitu_thread
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import base64

import os
import re
from models import *

from settings import *
import  urllib3
import re
import time
import os
import os.path
from urllib import request
import urllib
import hashlib

engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)

session = Session()


class Downloader(object):
    def start(self):
        while True:
            pictures = session.query(jiandan_meizitu).filter_by(status="new").all()
            for picture in pictures:
                self.download(picture)

            time.sleep(60)

    def download(self, picture):
        _ext = "." + picture.url.split("/")[-1].split(".")[-1]
        author = session.query(jiandan_meizitu).filter_by(id=picture.id).first().author.strip()
        dirname = os.path.join(DOWNLOADER_JIANDAN_DIR, author)
        if not os.path.exists(dirname):
            os.mkdir(dirname)
        req = request.Request(picture.url)
        req.add_header("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.")
        try:
            content = request.urlopen(req).read()
        except urllib.error.HTTPError as e:
            if e.getcode() == 404:
                picture.status = "notfound"
                session.commit()
                return
            else:
                print(e)
                raise Exception("Unknonw error")
        filename = hashlib.md5(content).hexdigest()
        picture.hash = filename
        print("正在下载 " + picture.url)
        with open(os.path.join(dirname, filename + "." + _ext), "wb") as f:
            f.write(content)
        picture.status = "complete"
        session.commit()

downloader = Downloader()
downloader.start()
